<?php

namespace App\Http\Controllers;

use App\Users;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;

class UserController extends Controller
{
	// 注册账号
    public function getAdds(Request $request)
	{
		 $this->validate($request, [
			'name' => 'required|unique:business|max:30',
			'phone' => 'required',
			'email' => 'required',
			'pwd' => 'required',
		]);
		
		if($request['pwd'] != $request['userpwd']){
			return ['msg'=>'两次密码不一致'];
		}
		$model = new Users();
		$data = $model->getAdds($request);
		if($data){
			$this->getEmail($request);
			$this->getPhone($request);
			return redirect('login');
		}else{
			return redirect('user-add');
		}
	}
	//登录账号
	public function getLogin(Request $request)
	{
		 $this->validate($request, [
			'name' => 'required',
			'pwd' => 'required',
		]);

		$model = new Users();
		$data = $model->getLogin($request);
		if($data){
			return ['msg'=>'登录成功'];
		}else{
			return ['msg'=>'登录失败'];
		}
	}
	
	//发送邮箱
	public function getEmail($request)
	{
		 $msg = '';
		 $to = $request['email'];
		 $subject = $request['name'];
		 Mail::send(
		 'demo.email',['msg'=>$request],
		 function ($msg) use ($to,$subject){
			 $msg->to($to)->subject($subject);
		 });
	}
	
	//发送手机号
	public function getPhone($request)
	{
		$statusStr = array(
		"0" => "短信发送成功",
		"-1" => "参数不全",
		"-2" => "服务器空间不支持,请确认支持curl或者fsocket，联系您的空间商解决或者更换空间！",
		"30" => "密码错误",
		"40" => "账号不存在",
		"41" => "余额不足",
		"42" => "帐户已过期",
		"43" => "IP地址限制",
		"50" => "内容含有敏感词"
		);
		$smsapi = "http://api.smsbao.com/";
		$user = "aniu"; //短信平台帐号
		$pass = md5("@wzy123123123"); //短信平台密码
		$content="您的账号为".$request['name'].",您的密码为".$request['pwd'];//要发送的短信内容
		$phone = $request['phone'];//要发送短信的手机号码
		$sendurl = $smsapi."sms?u=".$user."&p=".$pass."&m=".$phone."&c=".urlencode($content);
		$result =file_get_contents($sendurl) ;
		return $result;
	}
}
